package com.atguigu.demo.importdata;

import com.atguigu.eduservice.EduApplication;
import com.atguigu.eduservice.entity.Items;
import com.atguigu.eduservice.entity.TbDcUser;
import com.atguigu.eduservice.entity.ZxbApply;
import com.atguigu.eduservice.entity.zhonghan.*;
import com.atguigu.eduservice.service.*;
import com.atguigu.eduservice.service.slave_dataserver.QcZxbApplyService;
import com.atguigu.eduservice.service.slave_dataserver.QianchengItemsService;
import com.atguigu.eduservice.service.zhonghan.*;
import com.atguigu.eduservice.service.zhonghan.impl.QC_ItemsConstruction1Service;
import com.atguigu.eduservice.service.zhonghan.impl.QC_SupplierContactsService;
import com.atguigu.eduservice.service.zhonghan.qiancheng.*;
import com.atguigu.eduservice.utils.ExcelCommon;
import com.atguigu.eduservice.utils.ExcelExport;
import com.atguigu.eduservice.utils.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.util.*;


/**
 * @author
 * @since 2018/12/13
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EduApplication.class)

@Slf4j
@MapperScan("com.atguigu.eduservice.mapper")
public
class module_copydata {
    //@Autowired
    //TbDcUserMapper tbDcUserMapper;
    @Autowired
    private TbDcUserService tbDcUserService;
    @Autowired
    private
    ItemsService itemsService;

    @Autowired
    private
    QianchengItemsService qianchengItemsService;


    @Autowired
    private
    CustomersService customersService;
    @Autowired
    private
    CustomerContactsService customerContactsService;

    @Autowired
    private
    QcZxbApplyService qczxbApplyService;

    @Autowired
    private
    ZxbApplyService zxbApplyService;

    @Autowired
    private
    ZxbApplyService_base zxbApplyServicebase;
//    @Autowired
//    //private
//    //Zh_CustomersService_base zhCustomersServiceBase;


    @Autowired
    private
    CustomerContactsBaseService customerContactsBaseService;

    @Autowired
    private
    ZhCustomerContactsBaseService zhcustomerContactsBaseService;
    @Autowired
    private QcSalesordersService qcSalesOrdersService;
    @Autowired
    private QC_SalesOrderslineService qcSalesOrderslineService;
    @Autowired
    private QC_SalesOrdersDeliveryService qcSalesOrdersDeliveryService;

    @Autowired
    private Zh_SalesordersService zhSalesOrdersService;
    @Autowired
    private Zh_SalesOrderslineService zhSalesOrderslineService;
    @Autowired
    private Zh_SalesOrdersDeliveryService zhSalesOrdersDeliveryService;

    @Autowired
    private QC_CustomerItemsService qcCustomerItemsService;
    @Autowired
    private Zh_CustomerItemsService zhCustomerItemsService;

    @Autowired
    private QC_cusotmerItemsQuoteService qcCusotmerItemsQuoteService;
    @Autowired
    private Zh_cusotmerItemsQuoteService zhCusotmerItemsQuoteService;

    @Autowired
    private QC_ItemsConstruction1Service qcItemsConstruction1Service;
    @Autowired
    private Zh_ItemsConstruction1Service zhItemsConstruction1Service;

    //公司产品
    @Autowired
    private QC_ItemsService qcItemsService;
    @Autowired
    private Zh_ItemsService zhItemsService;

    @Autowired
    private QC_ItemsQuoteService qcItemsQuoteService;
    @Autowired
    private Zh_ItemsQuoteService zhItemsQuoteService;

    @Autowired
    private QC_ItemsConstructionService qcItemsConstructionService;
    @Autowired
    private Zh_ItemsConstructionService zhItemsConstructionService;


    @Autowired
    private QC_CustomerService qcCustomerService;

    @Autowired
    private ZhCustomerService zhCustomerService;

    @Autowired
    private QC_CustomerBankService qcCustomerBankService;
    @Autowired
    private Zh_CustomerBankService zhCustomerBankService;

    @Autowired
    private QC_CustomerContactsService qcCustomerContactsService;
    @Autowired
    private Zh_CustomerContactsService zhCustomerContactsService;

    @Autowired
    private QC_SuppliersService qcSuppliersService;
    @Autowired
    private Zh_SuppliersService zhSuppliersService;

    @Autowired
    private QC_SupplierBankInfoService qcSupplierBankInfoService;
    @Autowired
    private Zh_SupplierBankInfoService zhSupplierBankInfoService;

    @Autowired
    private QC_SupplierContactsService qcSupplierContactsService;
    @Autowired
    private Zh_SupplierContactsService zhSupplierContactsService;


    @Test
    public
    void salesorders_scopy() {
        QueryWrapper<SalesOrders> wrapper = new QueryWrapper<>();
        wrapper.eq("salesorderno", "A79AF1512")
                .orderByAsc("created");
        List<SalesOrders> salesOrdersList = qcSalesOrdersService.list(wrapper);
        UpdateWrapper updatesub = new UpdateWrapper<>();
        QueryWrapper query = new QueryWrapper();
        for (SalesOrders salesOrders : salesOrdersList) {
            wrapper = new QueryWrapper<>();
            wrapper.eq("salesorderno", salesOrders.getSalesOrderNo());
            int count = zhSalesOrdersService.count(wrapper);
//            if (count>0) {
//                UpdateWrapper maindelrapper=new UpdateWrapper<>();
//                maindelrapper.eq("parentid",salesOrders.getRecordID());
//                zhSalesOrderslineService.remove(maindelrapper);
//                zhSalesOrdersDeliveryService.remove(maindelrapper);
//                maindelrapper=new UpdateWrapper<>();
//                maindelrapper.eq("recordid",salesOrders.getRecordID());
//                zhSalesOrdersService.remove(maindelrapper);
//
//
//            }
            if (count == 0) {
                zhSalesOrdersService.save(salesOrders);

                QueryWrapper<SalesOrdersline> subline = new QueryWrapper<>();
                subline.eq("parentid", salesOrders.getRecordID());


                List<SalesOrdersline> salesOrderslines = qcSalesOrderslineService.list(subline);
                if (salesOrderslines.size() > 0)
                    zhSalesOrderslineService.saveBatch(salesOrderslines);

                QueryWrapper<SalesOrdersDelivery> subdeliver = new QueryWrapper<>();
                subdeliver.eq("parentid", salesOrders.getRecordID());
                List<SalesOrdersDelivery> salesOrdersDeliveryList = qcSalesOrdersDeliveryService.list(subdeliver);
                if (salesOrderslines.size() > 0)
                    zhSalesOrdersDeliveryService.saveBatch(salesOrdersDeliveryList);
            }
            //复制客户信息
            List<String> excludeFields = Arrays.asList("cpyj", "cpyj1", "cpyj2", "cplx","gsxyzed","sxedUsed","sxedRemain",
            "keyAccountManager","salesRap","tcbl","djzed","djUsed","djRemin");

            query = new QueryWrapper();
            //query.select(Customers.class, info -> !excludeFields.contains(info.getProperty()))
           // query.eq("customerno", salesOrders.getCustomerNo());

                Customers customers = qcCustomerService.getOne(new QueryWrapper<Customers>()
                        .select(Customers.class, info -> !excludeFields.contains(info.getProperty()))
                        .eq("customerno", salesOrders.getCustomerNo())


                );
            //System.out.println(customers);
            if (!Strings.isEmpty(customers.getCustomerNo())&& !customers.getCustomerNo().isEmpty()){
                zhCustomerService.save(customers);
                query = new QueryWrapper();
                query.eq("parentid", customers.getRecordID());
                List<CustomerBank> customerBankList = qcCustomerBankService.list(query);
                if (customerBankList.size() > 0)
                    zhCustomerBankService.saveBatch(customerBankList);
                List<CustomerContacts> customerContactsList = qcCustomerContactsService.list(query);
                if (customerContactsList.size() > 0)
                    zhCustomerContactsService.saveBatch(customerContactsList);

            }
            //复制客户产品



            List<CustomerItems> customerItemsList = qcCustomerItemsService.list(new QueryWrapper<CustomerItems>()
                    .select(CustomerItems.class, info -> !excludeFields.contains(info.getProperty()))
                    .inSql("CustomerItemNO", "select CustomerItemNO from SalesOrdersline inner join   salesorders on salesorders.recordid=SalesOrdersline.parentid where salesorders.recordid ='" + salesOrders.getRecordID() + "'")

                    .eq("customerno", salesOrders.getCustomerNo()));
            // System.out.println(customerItemsList);
            //客户产品查询
            for (CustomerItems customerItems : customerItemsList) {

                count = zhCustomerItemsService.count(new QueryWrapper<CustomerItems>()
                        .eq("CustomerItemNO", customerItems.getCustomerItemNO())
                        .eq("customerno", customerItems.getCustomerNo()));
                if (count == 0) {
                    zhCustomerItemsService.save(customerItems);
                    query = new QueryWrapper();

                    query.eq("parentid", customerItems.getRecordID());
                    List<ItemsQuote1> itemsQuote1List = qcCusotmerItemsQuoteService.list(query);
                    if (itemsQuote1List.size() > 0)
                        zhCusotmerItemsQuoteService.saveBatch(itemsQuote1List);
                    List<ItemsConstruction1> itemsConstruction1List = qcItemsConstruction1Service.list(query);
                    if (itemsConstruction1List.size() > 0)
                        zhItemsConstruction1Service.saveBatch(itemsConstruction1List);
                }
            }
                //复制产品信息

                //List<String> excludeFields = Arrays.asList("cpyj", "cpyj1", "cpyj2", "cplx");


                List<ZhItems> zhItemsList = qcItemsService.list(new QueryWrapper<ZhItems>()
                        .select(ZhItems.class, info -> !excludeFields.contains(info.getProperty()))
                        .inSql("ItemNO", "select ItemNO from SalesOrdersline inner join   salesorders on salesorders.recordid=SalesOrdersline.parentid where salesorders.recordid ='" + salesOrders.getRecordID() + "'"));


                // System.out.println(customerItemsList);
                //客户产品查询
                for (ZhItems zhItems : zhItemsList) {

                    count = zhItemsService.count(new QueryWrapper<ZhItems>()
                            .eq("ItemNO", zhItems.getItemNo()));

                    if (count == 0) {
                        zhItemsService.save(zhItems);
                        query = new QueryWrapper();

                        query.eq("parentid", zhItems.getRecordID());
                        List<ZhItemsQuote> itemsQuote1List = qcItemsQuoteService.list(query);
                        if (itemsQuote1List.size() > 0)
                            zhItemsQuoteService.saveBatch(itemsQuote1List);
                        List<ZhItemsConstruction> itemsConstruction1List = qcItemsConstructionService.list(query);
                        if (itemsConstruction1List.size() > 0)
                            zhItemsConstructionService.saveBatch(itemsConstruction1List);
                    }
                }
                    //***********复制工厂信息************
                    List<Suppliers> suppliersList = qcSuppliersService.list(new QueryWrapper<Suppliers>()
                            .select(Suppliers.class, info -> !excludeFields.contains(info.getProperty()))
                            .inSql("supplierno", "select supplierno from SalesOrdersline inner join   salesorders on salesorders.recordid=SalesOrdersline.parentid where salesorders.recordid ='" + salesOrders.getRecordID() + "'"));


                    for (Suppliers suppliers : suppliersList) {

                        count = zhSuppliersService.count(new QueryWrapper<Suppliers>()
                                .eq("supplierno", suppliers.getSupplierNo()));

                        if (count == 0) {
                            zhSuppliersService.save(suppliers);
                            query = new QueryWrapper();

                            query.eq("parentid", suppliers.getRecordID());
                            List<SupplierContacts> supplierContactsList = qcSupplierContactsService.list(query);
                            if (supplierContactsList.size() > 0)
                                zhSupplierContactsService.saveBatch(supplierContactsList);
                            List<BankInfo> bankInfoList = qcSupplierBankInfoService.list(query);
                            if (bankInfoList.size() > 0)
                                zhSupplierBankInfoService.saveBatch(bankInfoList);
                        }


                    }

                    System.out.println(salesOrders.getSalesOrderNo() + "已保存");

                    System.out.println(salesOrders.getSalesOrderNo() + "已处理");
                }
                //System.out.println(salesOrdersList);

            }
        }



